<template>
    <div class="usercontainer">
            <van-nav-bar
            left-text="返回"
            left-arrow
            @click-left="onClickLeft"
            @click-right="onClickRight">
            <van-button class="btnPrimary" type="primary" slot="right">提交</van-button>
            </van-nav-bar>
            <van-cell-group>
            <van-field v-model="authorizationCode"
            label="授权码"
            placeholder="请输入授权码"/>
            <van-field 
            v-model="userName"
            label="用户名"
            maxlength="10"
            show-word-limit
            placeholder="请输入用户名"/>
            </van-cell-group>
    </div>
</template>
<script>
import bobingService from '@/api/bobing-api.js'
import {getUser,setUser} from '@/utils/auth'
import {
  Tag,
  Col,
  Row,
  Icon,
  Cell,
  CellGroup,
  Toast,
  Button,
  Image,
  List,
  NavBar,
  Uploader,
  Field,
  Divider,
  Notify,
  Stepper,
} from 'vant'
export default {
    components: {
    [Tag.name]: Tag,
    [Col.name]: Col,
    [Icon.name]: Icon,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Button.name]:Button,
    [Row.name]:Row,
    [Toast.name]:Toast,
    [Image.name]:Image,
    [List.name]:List,
    [NavBar.name]:NavBar,
    [Uploader.name]:Uploader,
    [Field.name]:Field,
    [Divider.name]:Divider,
    [Notify.name]:Notify,
    [Stepper.name]:Stepper,
  },
    name: "EditUser",
    props:['showEditUser'],
    data() {
      return {
          currentUser:{},
          authorizationCode:'',
          userName:'',
      }
      },
    methods:{
        //返回
        onClickLeft(){
             this.$router.push(
                 {
                     path:'/Home',
                 })
        },
        onClickRight(){
            let _this = this
            if(!_this.authorizationCode){
                _this.$toast('请输入授权码')
                return
            }
            if(!_this.userName){
                _this.$toast('请输入用户名')
                return
            }
            _this.currentUser = {
                authorizationCode:_this.authorizationCode,
                userName:_this.userName,
            }
             bobingService.registerUser(_this.currentUser).then(response =>{
                 if(response.responseCode == 0){
                    _this.currentUser =  response.user
                    if(response.user)
                        setUser(response.user)
                    _this.$toast('注册成功')
                 }else if(response.responseCode == 1){
                     _this.$toast('注册失败')
                 }else if(response.responseCode == 2){
                     _this.$toast('授权码不存在')
                 }else if(response.responseCode == 3){
                     _this.$toast('内部服务错误')
                 }else if(response.responseCode == 4){
                     console.log(response.user)
                     if(response.user)
                        setUser(response.user)
                     _this.$toast('重新设置成功')
                 }
             },error =>{
                 _this.$toast('授权失败');
             })
        }
    },
    mounted(){
       let user = getUser()
       if(user){
           let _user = JSON.parse(user)
           this.currentUser = _user
           this.authorizationCode = this.currentUser.authorizationCode
           this.userName = this.currentUser.userName
       }
    }
}
</script>
<style scoped>
.btnPrimary{
    height: 30px;
}
.alert-tip{
    margin-left: 20px;
}
</style>